import { DEFAULT_FAST_MODEL } from '@/ai/constants/DefaultFastModel';
import { DEFAULT_SMART_MODEL } from '@/ai/constants/DefaultSmartModel';
import { type CurrentUserWorkspace } from '@/auth/states/currentUserWorkspaceState';
import { CUSTOM_WORKSPACE_APPLICATION_MOCK } from '@/object-metadata/hooks/__tests__/constants/CustomWorkspaceApplicationMock.test.constant';
import { type WorkspaceMember } from '@/workspace-member/types/WorkspaceMember';
import {
  FeatureFlagKey,
  OnboardingStatus,
  PermissionFlagType,
  SubscriptionInterval,
  SubscriptionStatus,
  type User,
  type Workspace,
  WorkspaceActivationStatus,
  WorkspaceMemberDateFormatEnum,
  WorkspaceMemberTimeFormatEnum,
} from '~/generated/graphql';
import { mockBillingPlans } from '~/testing/mock-data/billing-plans';
import { generatedMockObjectMetadataItems } from '~/testing/utils/generatedMockObjectMetadataItems';

type MockedUser = Pick<
  User,
  | 'id'
  | 'email'
  | 'firstName'
  | 'lastName'
  | 'canAccessFullAdminPanel'
  | 'canImpersonate'
  | '__typename'
  | 'supportUserHash'
  | 'onboardingStatus'
  | 'userVars'
  | 'availableWorkspaces'
  | 'hasPassword'
> & {
  workspaceMember: WorkspaceMember | null;
  locale: string;
  currentWorkspace: Workspace;
  workspaces: Array<{ workspace: Workspace }>;
  workspaceMembers: WorkspaceMember[];
  currentUserWorkspace: CurrentUserWorkspace;
};

export const avatarUrl =
  '';
export const workspaceLogoUrl =
  '';

// Extract Pro monthly base product from mockBillingPlans to use in workspace billing mocks
const PRO_PLAN = mockBillingPlans.listPlans.find((p) => p.planKey === 'PRO')!;
const PRO_BASE_LICENSED_PRODUCT = PRO_PLAN?.licensedProducts?.[0]!;
const PRO_BASE_MONTHLY_PRICE = PRO_BASE_LICENSED_PRODUCT?.prices?.find(
  (pr) => pr.recurringInterval === 'Month',
)!;
const PRO_METERED_PRODUCT = PRO_PLAN?.meteredProducts?.[0]!;
const PRO_METERED_MONTHLY_PRICE = PRO_METERED_PRODUCT?.prices?.find(
  (pr) => pr.recurringInterval === 'Month',
)!;

export const mockCurrentWorkspace = {
  workspaceCustomApplication: CUSTOM_WORKSPACE_APPLICATION_MOCK,
  workspaceCustomApplicationId: CUSTOM_WORKSPACE_APPLICATION_MOCK.id,
  subdomain: 'acme.twenty.com',
  id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6w',
  displayName: 'Twenty',
  inviteHash: 'twenty.com-invite-hash',
  logo: workspaceLogoUrl,
  isPublicInviteLinkEnabled: true,
  allowImpersonation: true,
  activationStatus: WorkspaceActivationStatus.ACTIVE,
  hasValidEnterpriseKey: false,
  isGoogleAuthEnabled: true,
  isPasswordAuthEnabled: true,
  isMicrosoftAuthEnabled: false,
  isCustomDomainEnabled: false,
  isPasswordAuthBypassEnabled: false,
  isGoogleAuthBypassEnabled: false,
  isMicrosoftAuthBypassEnabled: false,
  workspaceUrls: {
    customUrl: undefined,
    subdomainUrl: 'twenty.twenty.com',
  },
  featureFlags: [
    {
      key: FeatureFlagKey.IS_AIRTABLE_INTEGRATION_ENABLED,
      value: true,
    },
    {
      key: FeatureFlagKey.IS_POSTGRESQL_INTEGRATION_ENABLED,
      value: true,
    },
  ],
  createdAt: '2023-04-26T10:23:42.33625+00:00',
  updatedAt: '2023-04-26T10:23:42.33625+00:00',
  metadataVersion: 1,
  trashRetentionDays: 14,
  fastModel: DEFAULT_FAST_MODEL,
  smartModel: DEFAULT_SMART_MODEL,
  routerModel: 'auto',
  currentBillingSubscription: {
    __typename: 'BillingSubscription',
    id: '7efbc3f7-6e5e-4128-957e-8d86808cdf6a',
    interval: SubscriptionInterval.Month,
    status: SubscriptionStatus.Active,
    currentPeriodEnd: new Date().toString(),
    metadata: {
      plan: PRO_PLAN.planKey,
    },
    phases: [],
    billingSubscriptionItems: [
      {
        __typename: 'BillingSubscriptionItemDTO',
        id: '11111111-1111-4111-8111-111111111111',
        hasReachedCurrentPeriodCap: false,
        quantity: 1,
        stripePriceId: PRO_BASE_MONTHLY_PRICE.stripePriceId,
        billingProduct: {
          name: PRO_BASE_LICENSED_PRODUCT.name,
          description: PRO_BASE_LICENSED_PRODUCT.description,
          images: PRO_BASE_LICENSED_PRODUCT.images,
          metadata: PRO_BASE_LICENSED_PRODUCT.metadata,
        },
      },
      {
        __typename: 'BillingSubscriptionItemDTO',
        id: '11111111-1111-4111-8111-111111111112',
        hasReachedCurrentPeriodCap: false,
        quantity: null,
        stripePriceId: PRO_METERED_MONTHLY_PRICE.stripePriceId,
        billingProduct: {
          name: PRO_METERED_PRODUCT.name,
          description: PRO_METERED_PRODUCT.description,
          images: PRO_METERED_PRODUCT.images,
          metadata: PRO_METERED_PRODUCT.metadata,
        },
      },
    ],
  },
  billingSubscriptions: [
    {
      __typename: 'BillingSubscription',
      id: '7efbc3f7-6e5e-4128-957e-8d86808cdf6a',
      status: SubscriptionStatus.Active,
      metadata: {},
      phases: [],
      billingSubscriptionItems: [
        {
          __typename: 'BillingSubscriptionItemDTO',
          id: '22222222-2222-4222-8222-222222222222',
          hasReachedCurrentPeriodCap: false,
          quantity: 1,
          stripePriceId: PRO_BASE_MONTHLY_PRICE.stripePriceId,
          billingProduct: {
            name: PRO_BASE_LICENSED_PRODUCT.name,
            description: PRO_BASE_LICENSED_PRODUCT.description,
            images: PRO_BASE_LICENSED_PRODUCT.images,
            metadata: PRO_BASE_LICENSED_PRODUCT.metadata,
          },
        },
      ],
    },
  ],
  workspaceMembersCount: 1,
  databaseSchema: '',
  databaseUrl: '',
  isTwoFactorAuthenticationEnforced: false,
  __typename: 'Workspace',
} as const satisfies Workspace;

export const mockedWorkspaceMemberData: WorkspaceMember = {
  __typename: 'WorkspaceMember',
  id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6b',
  colorScheme: 'Light',
  avatarUrl,
  locale: 'en',
  name: {
    firstName: 'Charles',
    lastName: 'Test',
  },
  createdAt: '2023-04-26T10:23:42.33625+00:00',
  updatedAt: '2023-04-26T10:23:42.33625+00:00',
  userId: '2603c1f9-0172-4ea6-986c-eeaccdf7f4cf',
  userEmail: 'charles@test.com',
  dateFormat: WorkspaceMemberDateFormatEnum.DAY_FIRST,
  timeFormat: WorkspaceMemberTimeFormatEnum.HOUR_24,
  timeZone: 'America/New_York',
};

export const mockedUserData: MockedUser = {
  id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6d',
  __typename: 'User',
  email: 'charles@test.com',
  firstName: 'Charles',
  lastName: 'Test',
  canAccessFullAdminPanel: false,
  canImpersonate: false,
  hasPassword: true,
  supportUserHash:
    'a95afad9ff6f0b364e2a3fd3e246a1a852c22b6e55a3ca33745a86c201f9c10d',
  workspaceMember: mockedWorkspaceMemberData,
  currentWorkspace: mockCurrentWorkspace,
  currentUserWorkspace: {
    permissionFlags: [PermissionFlagType.WORKSPACE_MEMBERS],
    twoFactorAuthenticationMethodSummary: [],
    objectsPermissions: generatedMockObjectMetadataItems.map((item) => ({
      objectMetadataId: item.id,
      canReadObjectRecords: true,
      canUpdateObjectRecords: true,
      canSoftDeleteObjectRecords: true,
      canDestroyObjectRecords: true,
      restrictedFields: {},
    })),
  },
  locale: 'en',
  workspaces: [{ workspace: mockCurrentWorkspace }],
  workspaceMembers: [mockedWorkspaceMemberData],
  onboardingStatus: OnboardingStatus.COMPLETED,
  availableWorkspaces: {
    availableWorkspacesForSignIn: [],
    availableWorkspacesForSignUp: [],
  },
  userVars: {},
};

export const mockedLimitedPermissionsUserData: MockedUser = {
  ...mockedUserData,
  currentUserWorkspace: {
    ...mockedUserData.currentUserWorkspace,
    objectsPermissions: generatedMockObjectMetadataItems
      .filter(
        (objectMetadata) =>
          objectMetadata.nameSingular !== 'task' &&
          objectMetadata.nameSingular !== 'opportunity',
      )
      .map((item) => ({
        objectMetadataId: item.id,
        canReadObjectRecords: true,
        canUpdateObjectRecords: true,
        canSoftDeleteObjectRecords: true,
        canDestroyObjectRecords: true,
        restrictedFields: {},
      })),
  },
};

export const mockedOnboardingUserData = (
  onboardingStatus?: OnboardingStatus,
) => {
  return {
    id: '7dfbc3f7-6e5e-4128-957e-8d86808cdf6d',
    __typename: 'User',
    email: 'workspace-onboarding@test.com',
    firstName: '',
    lastName: '',
    hasPassword: false,
    canAccessFullAdminPanel: false,
    canImpersonate: false,
    supportUserHash:
      '4fb61d34ed3a4aeda2476d4b308b5162db9e1809b2b8277e6fdc6efc4a609254',
    workspaceMember: null,
    workspaceMembers: [],
    currentWorkspace: mockCurrentWorkspace,
    currentUserWorkspace: {
      permissionFlags: [PermissionFlagType.WORKSPACE_MEMBERS],
      objectPermissions: generatedMockObjectMetadataItems.map((item) => ({
        objectMetadataId: item.id,
        canReadObjectRecords: true,
        canUpdateObjectRecords: true,
        canSoftDeleteObjectRecords: true,
        canDestroyObjectRecords: true,
        restrictedFields: {},
      })),
    },
    locale: 'en',
    workspaces: [{ workspace: mockCurrentWorkspace }],
    onboardingStatus: onboardingStatus || null,
    userVars: {},
    availableWorkspaces: {
      availableWorkspacesForSignIn: [],
      availableWorkspacesForSignUp: [],
    },
  };
};
